This specification relates to compiling computer programs that contain multiversioned functions.
Modern computer programs are typically written in a high-level programming language. A high-level programming language allows data structures and algorithms in a computer program to be expressed in a form that can be easily read and understood by a human programmer. A software tool, called a “compiler,” can translate a computer program written in a high-level programming language into a set of intermediate code instructions or a set of low-level machine instructions, which can be executed by a computer's hardware. The program written in the high-level programming language is called the “source code.” The set of low-level machine instructions is called “object code.” In practice, a program typically includes multiple source code files from which a number of object code files can be derived. These multiple object code files and various libraries that include standard routines can be linked by a “linker” to create a single executable program. The executable program can then be executed on a computer.
Compilers can often perform various optimizations during program compilation to produce better performing object code, and consequently, more efficient executable programs. Common optimization techniques include loop optimization, data flow optimization, dead code elimination, inline expansion, constant propagation, and so on. The scope of code optimization can be intra-procedural, i.e., within a single procedure or code block, or inter-procedural, i.e., across multiple procedures or even multiple source code files.
Frequently executed functions are often compiled into different versions to take advantage of specific capabilities that are only present in some of the platforms on which the program is to be executed. Such capabilities, referred to in this description as “platform capabilities,” include different central processing units (CPUs), different co-processors, different software platforms, or other environmental aspects. For example, a first version may be optimized for execution on a platform that supports the Streaming SIMD Extension 3 (SSE3) instruction set, and a second version may be optimized for execution on a platform that supports the Streaming SIMD Extension 4 (SSE4) instruction set.
Some compilers implement “function multiversioning” and generate code that includes instructions for multiple versions of a function, each optimized for different platform capabilities. Function multiversioning comes with the cost of determining at runtime which of the multiple versions should be executed. That is, each time a function with multiple versions is called, a dispatch check of the platform capabilities is performed so that dispatch to the appropriate version occurs.